"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库date类型
Case Name   : 不符合的date类型转换
Create At   : 2023.08.09
Owner       : @wan005
Description :
    1、创建数据库
    2、不符合的date类型转换
    3、清理环境
Expect      :
    1、成功
    2、成功，弹出告警
    3、成功
History     : modified by @qumin20145，2024.01.31，弹出告警信息为空，修改用例
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class CompatibilityTest039(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = "db_date_0039"
        self.tb_name = "tb_date_0039"

    def test_atan(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建数据库 expect:成功-----'
        self.log.info(text)
        create_cmd = f"drop database if exists {self.db_name};" \
                     f"create database {self.db_name} dbcompatibility 'b';"
        msg = self.sh_primary.execut_db_sql(create_cmd)
        self.log.info(msg)
        self.assertIn('CREATE DATABASE', msg, '执行失败:' + text)

        text = "---step2:不符合的date类型转换 expect:成功，弹出告警返回--"
        self.log.info(text)
        sql_cmd = f"select 100::date;" \
                  f"select 10229::date;" \
                  f"select 100000000::date;" \
                  f"select 21000229::date;"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(msg)
        self.assertEqual(4, msg.count('WARNING'), '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(msg)
        self.assertIn('DROP DATABASE', msg, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
